package com.zhangyj.plugin;

import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.zhangyj.db.DBUtil;
import com.zhangyj.db.Database;

public class CostAccount extends StorageBase {

	private static final String sql_rukuxul_insert = "INSERT rukuxul (serialnum,entid,orgid,costorgid,billno,billcode,prefix,dates,businessid,shpid,billsn,rukshl,rukdj,chkshl,wdid,hwid) VALUES(?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?)";
	private static final String sql_rukuxul_update = "UPDATE rukuxul SET chkshl = chkshl + ? WHERE serialnum = ?";
	private static final String sql_rukuxul_delete = "DELETE FROM rukuxul WHERE serialnum = ?";
	private static final String sql_Chkuxul_load = "SELECT serialnum,entid,orgid,costorgid,billno,billcode,prefix,dates,businessid,shpid,billsn,chkshl,rkplh,ruxh,rkbillid,rkbillcode,rkywbs,rkdj,rkrq,rkdwid,rukshl,wdid,hwid FROM chkuxul WHERE  billno=? AND billsn=? ORDER BY serialnum";
	private static final String sql_Chkuxul_insert = "INSERT INTO chkuxul (serialnum,entid,orgid,costorgid,billno,billcode,prefix,dates,businessid,shpid,billsn,chkshl,rkplh,ruxh,rkbillid,rkbillcode,rkywbs,rkdj,rkrq,rkdwid,rukshl,wdid,hwid) VALUES (?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?,?)";
	private static final String sql_Chkuxul_update = "UPDATE chkuxul SET chkshl=chkshl + ? WHERE SerialNum=?";
	private static final String sql_Chkuxul_delete = "DELETE FROM chkuxul WHERE serialnum = ?";
	// 以下部分，都是记录明细数据中内容，必须是一个数据集
	// 业务标识：区分不同业务行为的标志，

	GoodsLiuShuiHz hz;
	List<GoodsLiuShuiMx> mxs;
	GoodsLiuShuiMx mx;
	Database dbc;
	private String dwid; // 单位ID：这笔商品库存发生变化的对方单位，可以为空；
	private String billcode;// 单据编号
	private String billid;// 单据ID
	private String ywbs_description;// 业务标识：区分不同业务行为的标志，比如：采购、销售、退回等；
	private int ywbs = 0x0;
	/** 货主Id */
	private String ownerId;
	/** 核算中心Id */
	String costOrgId = null;
	/** 成本核算级别 */
	String hesjb = null;

	public CostAccount() {
	}

	public String execute() throws ComponentException {
		try {

			for (Iterator<GoodsLiuShuiMx> iter = mxs.iterator(); iter.hasNext();) {
				mx = iter.next();
				account();
			}
			return null;
		} catch (Exception e) {
			throw new ComponentException(e);
		}
	}

	private void account() throws Exception {
		String spid = mx.getSpid();
		String ishs = "N";
		hesjb = null;
		int hesidCode = 0;// 成本核算方式ID代码
		PreparedStatement ps = dbc.prepareStatement("SELECT ishs,hesid,hesjb FROM shpattr WHERE shpid=?");
		ps.setString(1, spid);
		ResultSet rst = ps.executeQuery();
		if (rst.next()) {
			ishs = rst.getString(1); // 是否成本核算
			hesidCode = rst.getInt(2);// 成本核算方式ID
			hesjb = rst.getString(3); // 成本核算级别
		}
		rst.close();
		ps.close();
		double cksl = mx.getCukushul();
		if ("Y".equals(ishs) && (ywbs == YWBS_XIAO_SHOU || ywbs == YWBS_WAI_DIAO_CHU_KU) && cksl > 0) {
			double cbhssl = cksl;
			int ybillno = mx.getYbillno();
			int ybillsn = mx.getYbillsn();
			ps = dbc.prepareStatement("SELECT yhsshl,shpyqrsl FROM shpxswlmx WHERE billno=? and billsn=?");
			ps.setInt(1, ybillno);
			ps.setInt(2, ybillsn);
			rst = ps.executeQuery();
			if (rst.next()) {
				cbhssl = cbhssl - rst.getDouble(1) + rst.getDouble(2);
			}
			rst.close();
			ps.close();
			String shpid = mx.getSpid();// getValue(FIELDINDEX_SHPID);
			// 当期核算参数
			double[] param = null;
			// 企业总核算
			double[] zhszxparam = this.getCaculateParam("shpentjc", "entid", hz.getEntid(), shpid);
			// 成本核算级别 :A：货位核算；B：机构核算；C：区域核算中心；D：企业总核算
			if ("A".equals(hesjb)) {
				// 货位核算，没有最高、最低、最后进价，也没有固定成本和综合毛利；
				ps = dbc.prepareStatement("SELECT hescbj FROM shphwjc WHERE hwid=? AND shpid=? AND ownerid=?");
				ps.setString(1, mx.getHuowei());
				ps.setString(2, shpid);

				ps.setString(3, ownerId);
				rst = ps.executeQuery();
				double hescbj = 0d;
				if (rst.next()) {
					hescbj = rst.getDouble(1);
				}
				rst.close();
				ps.close();
				param = new double[] { hescbj, 0d, 0d, 0d, 0d, 0d };
			} else if ("B".equals(hesjb)) {
				param = this.getCaculateParam("shporgjc", "orgid", hz.getOrgid(), shpid);
			} else if ("C".equals(hesjb)) {
				if (costOrgId != null && !costOrgId.isEmpty()) {
					param = this.getCaculateParam("shphszx", "orgid", costOrgId, shpid);
				}
			} else if ("D".equals(hesjb)) {
				param = new double[zhszxparam.length];
				System.arraycopy(zhszxparam, 0, param, 0, zhszxparam.length);
			}
			account(hesidCode, shpid, cbhssl, costOrgId, param, zhszxparam);
		} else if (((mx.isChonghong() && (ywbs == YWBS_CAI_GOU || ywbs == YWBS_WAI_DIAO_RU_KU)) || ywbs == YWBS_CAI_GOU_TUI_CHU || ywbs == YWBS_WAI_DIAO_HUI_CHU) && mx.getRukushul() > 0
				&& mx.getYbillno() > 0 && mx.getYbillsn() > 0) {
			rukuch(mx.getRukushul());
		} else if ((mx.isChonghong() && (ywbs == YWBS_XIAO_SHOU || ywbs == YWBS_WAI_DIAO_CHU_KU)) || ywbs == YWBS_XIAO_SHOU_HUI_TUI || ywbs == YWBS_WAI_DIAO_HUI_TUI) {
			chukch(mx.getCukushul());
		}
	}

	/**
	 * 处理涉及入库序列的冲红 <br>
	 * 采购单冲红或进货退出时，如果遇到商品成本计算方法为“先进先出，后进先出，高进先出，低进先出，本期先进先出，本期后进先出”的商品，才需要进行冲红，
	 * 否则，是不需要冲红的；
	 */
	private void rukuch(double rukshl) throws Exception {
		String sql = "SELECT serialnum,rukshl,chkshl FROM rukuxul WHERE billno=? AND billsn=? ";
		int rukuxul_serialnum = -1;
		double rukuxul_rukshl = 0d;
		double rukuxul_chkshl = 0d;

		PreparedStatement ps = dbc.prepareStatement(sql);
		ps.setInt(1, mx.getYbillno());
		ps.setInt(2, mx.getYbillsn());
		ResultSet rst = ps.executeQuery();
		if (rst.next()) {
			rukuxul_serialnum = rst.getInt(1);
			rukuxul_rukshl = rst.getDouble(2);
			rukuxul_chkshl = rst.getDouble(3);
		}
		rst.close();
		ps.close();
		if (rukuxul_serialnum != -1) {
			if (maxZore(rukuxul_rukshl - rukuxul_chkshl + rukshl)) {
				ps = dbc.prepareStatement(sql_rukuxul_update);
				ps.setDouble(1, rukshl);
				ps.setInt(2, rukuxul_serialnum);
				ps.executeUpdate();
			} else {
				ps = dbc.prepareStatement(sql_rukuxul_delete);
				ps.setInt(1, rukuxul_serialnum);
				ps.executeUpdate();
			}
			ps.close();
		}
		ps = null;
	}

	private void chukch(double chkshl) throws Exception {

		PreparedStatement ps = dbc.prepareStatement(sql_Chkuxul_load);
		ps.setInt(1, mx.getYbillno());
		ps.setInt(2, mx.getYbillsn());
		ResultSet rst = ps.executeQuery();
		List<Chkuxul> list = new ArrayList<Chkuxul>();
		while (rst.next()) {
			list.add(builderChkuxul(rst));
		}
		rst.close();
		ps.close();
		for (int i = 0; i < list.size(); i++) {
			Chkuxul chkuxul = list.get(i);
			double chkuxul_chkshl = chkuxul.chkshl;
			if (maxZore(chkshl + chkuxul_chkshl)) {
				ps = dbc.prepareStatement(sql_Chkuxul_update);
				ps.setDouble(1, chkshl);
				ps.setInt(2, chkuxul.serialnum);
				ps.executeUpdate();
				ps.close();

				if (!"无勾销数据".equals(chkuxul.rkbillcode)) {
					updateOrinsertRuKuxul(chkuxul, Math.abs(chkshl));
				}
				break;
			} else {
				ps = dbc.prepareStatement(sql_Chkuxul_delete);
				ps.setInt(1, chkuxul.serialnum);
				ps.executeUpdate();
				ps.close();

				if (!"无勾销数据".equals(chkuxul.rkbillcode)) {
					updateOrinsertRuKuxul(chkuxul, chkuxul.chkshl);
				}
				chkshl = chkshl + chkuxul.chkshl;
			}
		}
	}

	/**
	 * 
	 * @param hesid
	 *            成本核算方式 *
	 * @param shpid
	 *            商品Id
	 * @param cbhssl
	 *            成本核算数量
	 * @param chbparam
	 *            核算成本参数
	 * @param entchbparam
	 *            企业核算成本参数
	 */

	private void account(int hesidCode, String shpid, double cbhssl, String costOrgId, double chbparam[], double entchbparam[]) throws Exception {
		double chkje = 0d;// 出库金额
		double hscbj = chbparam[0];// 核算成本价

		if (JIA_QUAN_YI_DONG == hesidCode) {// 加权移动
			chkje = chbparam[0] * cbhssl;
		} else if (PI_HAO_JI_JIA == hesidCode) {// 批号计价
			String sql = "SELECT gebjj FROM BatchCode WHERE entid=? AND shpid=? AND wdid=?";
			PreparedStatement ps = dbc.prepareStatement(sql);
			ps.setString(1, hz.getEntid());
			ps.setString(2, shpid);
			ps.setString(3, mx.getWeiduid());
			ResultSet rst = ps.executeQuery();
			if (rst.next()) {
				double gebjj = rst.getDouble(1);// 个别计价
				if (maxZore(gebjj)) {
					hscbj = gebjj;
				}
			}
			rst.close();
			chkje = hscbj * cbhssl;
		} else if (PI_CI_HE_SUAN == hesidCode) {// 批次核算
			String sql = "SELECT jj FROM shppici WHERE shpid=? AND SerialNum=?";
			PreparedStatement ps = dbc.prepareStatement(sql);
			ps.setString(1, shpid);
			ps.setInt(2, mx.getPici());
			ResultSet rst = ps.executeQuery();
			if (rst.next()) {
				double cgdj = rst.getDouble(1);// 采购单价
				if (maxZore(cgdj)) {
					hscbj = cgdj;
				}
			}
			rst.close();
			chkje = hscbj * cbhssl;
		} else {
			for (int i = 0; i < chbparam.length; i++) {
				// 如果小于零，给企业的
				if (minZore(chbparam[i]))
					chbparam[i] = entchbparam[i];
			}
			if (hesidCode == ZUI_GAO_JIN_JIA) {// 最高进价
				hscbj = chbparam[1];
				chkje = cbhssl * hscbj;
			} else if (hesidCode == ZUI_DI_JIN_JIA) {// 最低进价
				hscbj = chbparam[2];
				chkje = cbhssl * hscbj;
			} else if (hesidCode == ZUI_HOU_JIN_JIA) {// 最后进价
				hscbj = chbparam[3];
				chkje = cbhssl * hscbj;
			} else if (hesidCode == GU_DING_CHENG_BENG) {// 固定成本
				hscbj = chbparam[4];
				chkje = cbhssl * hscbj;
			} else if (hesidCode == ZHONG_HE_MAO_LI) {// 综合毛利
				hscbj = mx.getXiaosoudanjia() * (1 - chbparam[5] / 100);
				chkje = cbhssl * hscbj;
			} else {
				if ((hesidCode & 0x3F) == hesidCode) {
					chkje = rukuxulAccount(hesidCode, shpid, costOrgId, cbhssl, chbparam);
				}
			}

		}
		double maoli = mx.getBuhansuijine() - chkje;
		updateShpxswlmx(mx.getYbillno(), mx.getYbillsn(), cbhssl, chkje);
		mx.setMaoli(maoli);
		mx.setCukujine(chkje);
	}

	private double rukuxulAccount(int hesidCode, String shpid, String costOrgId, double cbhssl, double chbparam[]) throws Exception {
		StringBuilder sql = new StringBuilder(128);
		sql.append("SELECT serialnum,orgid,costorgid,billno,billcode,prefix,dates,businessid,shpid,billsn,rukshl,rukdj,chkshl,wdid,hwid FROM rukuxul WHERE shpid=?");
		String paramVal = null;
		boolean hasParam = false;
		if ("C".equals(hesjb) && costOrgId != null && costOrgId.length() > 0) {
			sql.append(" AND costorgid=?");
			paramVal = costOrgId;
			hasParam = true;
		} else if ("B".equals(hesjb)) {
			sql.append(" AND orgId=?");
			paramVal = hz.getOrgid();
			hasParam = true;
		}

		if (hesidCode == FIRST_IN_FIRST_OUT || hesidCode == BQ_FIRST_IN_FIRST_OUT) {
			sql.append(" order by dates,serialnum");
		} else if (hesidCode == LAST_IN_FIRST_OUT || hesidCode == BQ_LAST_IN_FIRST_OUT) {
			sql.append(" order by dates,serialnum desc");
		} else if (hesidCode == HIGH_IN_FIRST_OUT) {
			sql.append(" order by rukdj,serialnum desc");
		} else if (hesidCode == LOW_IN_FIRST_OUT) {
			sql.append(" order by rukdj,serialnum");
		}
		PreparedStatement ps = dbc.prepareStatement(sql.toString());
		ps.setString(1, shpid);
		if (hasParam) {
			ps.setString(2, paramVal);
		}
		double cukshl = cbhssl;
		double chkuje = 0d;
		ResultSet rukuxulie = ps.executeQuery();
		while (rukuxulie.next()) {
			Rukuxul rukuxul = builderRukuxul(rukuxulie);
			Chkuxul chkuxul = new Chkuxul(rukuxul);
			chkuxul.entid = hz.getEntid();
			chkuxul.orgid = hz.getOrgid();
			chkuxul.billno = Integer.parseInt(this.billid);
			chkuxul.billcode = this.billcode;
			chkuxul.prefix = this.ywbs_description;
			chkuxul.dates = hz.getDate();
			chkuxul.businessid = this.dwid;
			chkuxul.shpid = shpid;
			chkuxul.billsn = mx.getBillsn();// getIntValue(FIELDINDEX_BILLSN);
			if (rukuxul.rukshl > cukshl) {// 勾销完毕；
				chkuxul.chkshl = cukshl;
				updateRukuxul(cukshl, rukuxul.serialnum);
				chkuje = chkuje + cukshl * rukuxul.rukdj;
				cukshl = 0d;
				break;
			} else {
				deleteRukuxul(rukuxul.serialnum);
				chkuxul.chkshl = rukuxul.rukshl;
				cukshl = cukshl - rukuxul.rukshl;
				chkuje = chkuje + rukuxul.rukshl * rukuxul.rukdj;
			}
			insertChkuxul(chkuxul);
		}
		rukuxulie.close();
		if (maxZore(cukshl)) {// 没有勾销完毕
			Chkuxul chkuxul = new Chkuxul();
			chkuxul.entid = hz.getEntid();
			chkuxul.orgid = hz.getOrgid();
			chkuxul.costorgid = costOrgId;
			chkuxul.billno = Integer.parseInt(this.billid);
			chkuxul.billcode = this.billcode;
			chkuxul.prefix = this.ywbs_description;
			chkuxul.dates = hz.getDate();
			chkuxul.businessid = this.dwid;
			chkuxul.shpid = shpid;
			chkuxul.chkshl = cukshl;
			chkuxul.rkbillcode = "无勾销数据";
			insertChkuxul(chkuxul);
			chkuje = chkuje + cukshl * chbparam[0];
		}
		return chkuje;
	}

	/**
	 * 
	 * @param tbname
	 *            取值的表名
	 * @param fldname
	 *            条件的字段名
	 * @param fldVal
	 *            字段值
	 * @param shpid
	 *            商品id
	 * @return double 类型的数组，长度为6 各个下标代表的值: 默认值是0d<br>
	 *         0: 核算成本单价<br>
	 *         1: 最高进价<br>
	 *         2: 最低进价<br>
	 *         3: 最后进价<br>
	 *         4: 固定成本<br>
	 *         5: 毛利率<br>
	 * @throws SQLException
	 */
	private double[] getCaculateParam(String tbname, String fldname, String fldVal, String shpid) throws SQLException {
		double[] returnParam = new double[] { 0d, 0d, 0d, 0d, 0d, 0d };
		StringBuilder sql = new StringBuilder(100);
		sql.append("SELECT hescbj,zgjj,zdjj,zhjj,gudchb,maolv FROM ").append(tbname).append(" WHERE ").append(fldname).append(" =? AND shpid=?");
		PreparedStatement ps = dbc.prepareStatement(sql.toString());
		ps.setString(1, fldVal);
		ps.setString(2, shpid);
		ResultSet rst = ps.executeQuery();
		if (rst.next()) {
			int index = 0;
			returnParam[index] = rst.getDouble(++index);// 核算成本单价;
			returnParam[index] = rst.getDouble(++index);// 最高进价 :=0
			returnParam[index] = rst.getDouble(++index);// 最低进价 :=0
			returnParam[index] = rst.getDouble(++index);// 最后进价 :=0
			returnParam[index] = rst.getDouble(++index);// 固定成本 :=0
			returnParam[index] = rst.getDouble(++index);// 毛利率 :=0
		}
		rst.close();
		rst = null;
		return returnParam;
	}

	private Rukuxul builderRukuxul(ResultSet rst) throws SQLException {
		Rukuxul rukuxul = new Rukuxul();
		int index = 1;
		rukuxul.serialnum = rst.getInt(index++);
		rukuxul.orgid = rst.getString(index++);
		rukuxul.costorgid = rst.getString(index++);
		rukuxul.billno = rst.getInt(index++);
		rukuxul.billcode = rst.getString(index++);
		rukuxul.prefix = rst.getString(index++);
		rukuxul.dates = rst.getString(index++);
		rukuxul.businessid = rst.getString(index++);
		rukuxul.shpid = rst.getString(index++);
		rukuxul.billsn = rst.getInt(index++);
		rukuxul.rukshl = rst.getDouble(index++);
		rukuxul.rukdj = rst.getDouble(index++);
		rukuxul.chkshl = rst.getDouble(index++);
		rukuxul.wdid = rst.getString(index++);
		rukuxul.hwid = rst.getString(index++);
		return rukuxul;
	}

	private Chkuxul builderChkuxul(ResultSet rst) throws SQLException {
		Chkuxul chkuxul = new Chkuxul();
		int index = 1;
		chkuxul.serialnum = rst.getInt(index++);
		chkuxul.entid = rst.getString(index++);
		chkuxul.orgid = rst.getString(index++);
		chkuxul.costorgid = rst.getString(index++);
		chkuxul.billno = rst.getInt(index++);
		chkuxul.billcode = rst.getString(index++);
		chkuxul.prefix = rst.getString(index++);
		chkuxul.dates = rst.getString(index++);
		chkuxul.businessid = rst.getString(index++);
		chkuxul.shpid = rst.getString(index++);
		chkuxul.billsn = rst.getInt(index++);
		chkuxul.chkshl = rst.getDouble(index++);
		chkuxul.rkplh = rst.getInt(index++);
		chkuxul.ruxh = rst.getInt(index++);
		chkuxul.rkbillid = rst.getInt(index++);
		chkuxul.rkbillcode = rst.getString(index++);
		chkuxul.rkywbs = rst.getString(index++);
		chkuxul.rkdj = rst.getDouble(index++);
		chkuxul.rkrq = rst.getString(index++);
		chkuxul.rkdwid = rst.getString(index++);
		chkuxul.rukshl = rst.getDouble(index++);
		chkuxul.wdid = rst.getString(index++);
		chkuxul.hwid = rst.getString(index++);
		return chkuxul;
	}

	private class Rukuxul {
		public int serialnum, billno, billsn;
		public String orgid, costorgid, billcode, prefix, dates, businessid, shpid, wdid, hwid;
		public double rukshl, rukdj, chkshl;
	}

	private class Chkuxul {
		public Chkuxul() {

		};

		public Chkuxul(Rukuxul rukuxul) {
			this.costorgid = rukuxul.costorgid;
			rkplh = rukuxul.serialnum;// 入库排列号ID
			ruxh = rukuxul.billsn; // 入库明细序号
			rkbillid = rukuxul.billno; // 入库单据ID
			rkbillcode = rukuxul.billcode; // 入库单据编号
			rkywbs = rukuxul.prefix; // 入库业务标识
			rkdj = rukuxul.rukdj; // 入库单价
			rkrq = rukuxul.dates;// 入库日期
			rkdwid = rukuxul.businessid; // 入库单位ID
			rukshl = rukuxul.rukshl; // 入库数量
			wdid = rukuxul.wdid; // 维度ID
			hwid = rukuxul.hwid; // 存储位置ID

			shpid = rukuxul.shpid;
			orgid = rukuxul.orgid;
			chkshl = rukuxul.chkshl;
		}

		int serialnum = 0;// 排列号
		int billno;// 单据ID
		int billsn;// 单据序号
		int rkplh;// 入库排列号ID
		int ruxh;// 入库明细序号
		int rkbillid;// 入库单据ID

		String entid;// 企业ID
		String orgid;// 机构ID
		String costorgid;// 成本中心ID
		String billcode;// 单据编号
		String prefix;// 业务标识
		String dates;// 日期
		String businessid;// 单位ID
		String shpid;// 商品ID
		String rkbillcode;// 入库单据编号
		String rkywbs;// 入库业务标识
		String rkrq;// 入库日期
		String rkdwid;// 入库单位ID
		String wdid;// 维度ID
		String hwid;// 存储位置ID

		double chkshl;// 出库数量
		double rkdj;// 入库单价
		double rukshl;// 入库数量

	}

	private void insertChkuxul(Chkuxul chkuxul) throws Exception {
		if (chkuxul.serialnum == 0) {
			chkuxul.serialnum = DBUtil.getIntId("chkuxul");// STIDGenerator.allocSerialNum(dbc,
															// "CKX",
															// "商品成本核算插件");
		}
		PreparedStatement ps = dbc.prepareStatement(sql_Chkuxul_insert);
		int index = 1;
		ps.setInt(index++, chkuxul.serialnum);
		ps.setString(index++, chkuxul.entid);
		ps.setString(index++, chkuxul.orgid);
		ps.setString(index++, chkuxul.costorgid);
		ps.setInt(index++, chkuxul.billno);
		ps.setString(index++, chkuxul.billcode);
		ps.setString(index++, chkuxul.prefix);
		ps.setString(index++, chkuxul.dates);
		ps.setString(index++, chkuxul.businessid);
		ps.setString(index++, chkuxul.shpid);
		ps.setInt(index++, chkuxul.billsn);
		ps.setBigDecimal(index++, new BigDecimal(chkuxul.chkshl));
		ps.setInt(index++, chkuxul.rkplh);
		ps.setInt(index++, chkuxul.ruxh);
		ps.setInt(index++, chkuxul.rkbillid);
		ps.setString(index++, chkuxul.rkbillcode);
		ps.setString(index++, chkuxul.rkywbs);
		ps.setBigDecimal(index++, new BigDecimal(chkuxul.rkdj));
		ps.setString(index++, chkuxul.rkrq);
		ps.setString(index++, chkuxul.rkdwid);
		ps.setBigDecimal(index++, new BigDecimal(chkuxul.rukshl));
		ps.setString(index++, chkuxul.wdid);
		ps.setString(index++, chkuxul.hwid);
		ps.execute();
		ps.close();
		ps = null;
	}

	private void updateOrinsertRuKuxul(Chkuxul chkuxul, double chkshl) throws Exception {
		String sql1 = "UPDATE rukuxul SET chkshl=chkshl + ? WHERE SerialNum=?";
		PreparedStatement ps = dbc.prepareStatement(sql1);
		ps.setDouble(1, chkshl);
		ps.setInt(2, chkuxul.rkplh);
		int count = ps.executeUpdate();
		ps.close();
		if (count > 0) {
			return;

		}
		ps = dbc.prepareStatement(sql_rukuxul_insert);
		int index = 1;
		ps.setInt(index++, chkuxul.rkplh);// serialnum
		ps.setString(index++, hz.getEntid());// entid
		ps.setString(index++, chkuxul.orgid);// orgid
		ps.setString(index++, chkuxul.costorgid);// costorgid
		ps.setInt(index++, chkuxul.billno);// billno
		ps.setString(index++, chkuxul.billcode);// billcode
		ps.setString(index++, chkuxul.prefix);// prefix
		ps.setString(index++, chkuxul.dates);// dates
		ps.setString(index++, chkuxul.businessid);// businessid
		ps.setString(index++, chkuxul.shpid);// shpid
		ps.setInt(index++, chkuxul.billsn);// billsn
		ps.setDouble(index++, chkuxul.rukshl);// rukshl
		ps.setDouble(index++, chkuxul.rkdj);// rukdj
		ps.setDouble(index++, chkshl);// chkshl
		ps.setString(index++, chkuxul.wdid);// wdid
		ps.setString(index++, chkuxul.hwid);// hwid
		ps.execute();
		ps.close();
	}

	public void updateShpxswlmx(int billno, int billsn, double chbhssl, double chkje) throws Exception {
		String sql = "SELECT COUNT(*) FROM shpxswlmx WHERE billno=? and billsn=?";
		PreparedStatement ps = dbc.prepareStatement(sql);
		ps.setInt(1, billno);
		ps.setInt(2, billsn);
		ResultSet rst = ps.executeQuery();
		boolean has = false;
		if (rst.next()) {
			int count = rst.getInt(1);
			if (count > 0) {
				has = true;
			}
		}
		rst.close();
		ps.close();
		if (has) {
			sql = "UPDATE shpxswlmx SET yhsshl= yhsshl + ? ,yhesje=yhesje+? WHERE billno=? and billsn=?";
			ps = dbc.prepareStatement(sql);
			ps.setDouble(1, chbhssl);
			ps.setDouble(2, chkje);
			ps.setInt(3, billno);
			ps.setInt(4, billsn);
			ps.executeUpdate();
			ps.close();
		}
		ps = null;
	}

	private void updateRukuxul(double chkshl, int serialnum) throws Exception {
		PreparedStatement ps = dbc.prepareStatement(sql_rukuxul_update);
		ps.setDouble(1, chkshl);
		ps.setInt(2, serialnum);
		ps.executeUpdate();
		ps.close();
		ps = null;
	}

	private void deleteRukuxul(int serialnum) throws Exception {
		PreparedStatement ps = dbc.prepareStatement(sql_rukuxul_delete);
		ps.setInt(2, serialnum);
		ps.executeUpdate();
		ps.close();
		ps = null;
	}

}
